﻿using System;
using System.Collections.Generic;

namespace TShooter.TfsSearch.EngineModel
{
    public abstract class IndexerBase : ServiceBase, IIndexer
    {
        private IEngine _Engine;
        private readonly Queue<DocumentReference> _Queue = new Queue<DocumentReference>();

        #region IIndexer Members

        public void EnqueueItems(IEnumerable<DocumentReference> references)
        {
            lock (_Queue)
            {
                foreach (DocumentReference reference in references)
                {
                    if (_Queue.Contains(reference))
                    {
                        _Queue.Enqueue(reference);
                    }
                }
            }
        }

        public IEngine Engine
        {
            get { return _Engine; }
            set { _Engine = value; }
        }

        public void Add(IndexDocument doc)
        {
            throw new NotImplementedException();
        }

        public int GetRevision(int id)
        {
            return GetDocumentRevision(id);
        }

        #endregion

        protected override bool DoService()
        {
            DocumentReference reference;

            lock (_Queue)
            {
                reference = _Queue.Dequeue();
            }

            if (reference != null)
            {
                Add(BuildDocument(reference.ID));
            }

            return (_Queue.Count > 0);
        }

        protected abstract IndexDocument BuildDocument(int id);
        protected abstract int GetDocumentRevision(int id);
    }
}